package com.example.fourinarow;

import java.util.Random;
//Class handling the Singleplayer AI.
public class GameAI {
	GameMatrix gm;
	Random random = new Random();
	private int randomNumb;

	public GameAI(GameMatrix gm) {
		this.gm = gm;
	}

//Method to generate an AI move depending on player move.
	public int getAIMove(int x) {
		int i = 0;
		boolean done = false;
		while (done == false) {
			getRandomMove(x);
			for (i = 5; i >= 0; i--) {
				if (gm.getFreePositionInColumn(i, x + randomNumb, 2)) {
					done = true;
					break;
				}
			}
			if(done)
				break;
			
			x = random.nextInt(7);
		}

		return (i * 7) + (x + randomNumb);
	}

	private void getRandomMove(int x) {

		if (x == 0) {
			randomNumb = random.nextInt(2);
		} else if (x == 6) {
			randomNumb = random.nextInt(2) - 1;
		} else {
			randomNumb = random.nextInt(3) - 1;
		}

	}

}
